package cn.jishiyu11.xeqb.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.NetworkInterface;
import java.net.SocketException;

/**
 * Created by vvguoliang on 2017/6/24.
 * <p>
 * APP 操作
 */
@SuppressWarnings("JavaDoc")
@SuppressLint("HardwareIds")
public class AppUtil {


    /**
     * 单例对象实例
     */
    private static class AppUtilHolder {
        static final AppUtil INSTANCE = new AppUtil();
    }

    public static AppUtil getInstance() {
        return AppUtil.AppUtilHolder.INSTANCE;
    }

    /**
     * private的构造函数用于避免外界直接使用new来实例化对象
     */
    private AppUtil() {
    }

    /**
     * 屏幕分辨率
     *
     * @param context
     * @return
     */
    public int[] Dispay(Activity context) {
        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        return new int[]{screenWidth, screenHeight};
    }

    public final int MY_PERMISSIONS_REQUEST_CAMERA = 100;//拍照权限
    public final int MY_PERMISSIONS_REQUEST_READ_SD = 101;//读SD卡权限
    public final int MY_PERMISSIONS_REQUEST_WRITE_SK = 102;//写SD卡权限
    public final int MY_PERMISSIONS_REQUEST_READ_SD_PHOTOALBUM = 103;//写SD卡权限
    public final int MY_PERMISSIONS_REQUEST_WRITE_SK_PHOTOALBUM = 104;//写SD卡权限
    public final int CAPTURE_IMAGE_REQUEST = 104;//拍照后的返回值
    public final int LOAD_IMAGE_REQUEST = 105;//相册的返回值
    public final int CLIP_IMAGE_REQUEST = 106;//剪裁图片的返回值
    public final String IMAGE_TYPE = "image/*";
    public File mOutFile;//图片uri路径
    public File mImageFile = null;//图片file路径
    public final int MY_PERMISSIONS_REQUEST_CONTACTS = 107;
    public final int MY_PERMISSIONS_PHONE_DIAL = 108;
    public final int MY_PERMISSIONS_PHONE_IMEI = 109;
    public final int MY_PERMISSIONS_PHONE_READWRITE = 110;
    private final int BAIDU_READ_PHONE_STATE = 111;//过去手机GPS

    public final int TIME = 2000;

//    public  String phoneDeviceId = "";
//    public  String AppVersion = "";


    public Integer mBuildVersion = android.os.Build.VERSION.SDK_INT;//当前SDK版本


    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称 版本号
     */
    public String getVersionName(int code, Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            if (code == 1) {
                return packageInfo.versionName;
            } else {
                return packageInfo.versionCode + "";
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void getManager(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * 获取屏幕分辨率
     *
     * @param context
     * @return
     */
    @SuppressWarnings("deprecation")
    public static int[] getScreenDispaly(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = windowManager.getDefaultDisplay().getWidth();// 手机屏幕的宽度
        int height = windowManager.getDefaultDisplay().getHeight();// 手机屏幕的高度
        int result[] = {width, height};
        return result;
    }


    private static final String CHANNEL_KEY = "UMENG_CHANNEL";
    private static String mChannel;

    /**
     * 返回市场。 如果获取失败返回defaultChannel
     *
     * @param context
     * @return
     */
    public String getChannel(Context context, int id_channle) {
        // 从apk中获取
        mChannel = getChannelFromApk1(context, CHANNEL_KEY);
        return mChannle(id_channle, mChannel);
    }

    private String getChannelFromApk1(Context context, String channelKey) {
        try {
            ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            return appInfo.metaData.getString(channelKey);
        } catch (Exception exception) {
            return null;
        }
    }

    /**
     * 转二进制
     *
     * @param bm
     * @return
     */
    public byte[] bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public String getMacAddress() {
        String macAddress;
        StringBuffer buf = new StringBuffer();
        NetworkInterface networkInterface;
        try {
            networkInterface = NetworkInterface.getByName("eth1");
            if (networkInterface == null) {
                networkInterface = NetworkInterface.getByName("wlan0");
            }
            if (networkInterface == null) {
                return "02:00:00:00:00:02";
            }
            byte[] addr = networkInterface.getHardwareAddress();
            for (byte b : addr) {
                buf.append(String.format("%02X:", b));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            macAddress = buf.toString();
        } catch (SocketException e) {
            e.printStackTrace();
            return "02:00:00:00:00:02";
        }
        return macAddress;
    }


    /**
     * 渠道id
     */
    private String mChannle(int id_channle, String channle) {
        if ("QD0163".equals(channle)) {
            if (id_channle == 1) {
                return "卡小咪-应用宝" + "QD0163";
            } else {
                return "QD0163";
            }
        } else if ("QD0164".equals(channle)) {
            if (id_channle == 1) {
                return "卡小咪-VIVO" + "QD0164";
            } else {
                return "QD0164";
            }
        } else if ("QD0165".equals(channle)) {
            if (id_channle == 1) {
                return "卡小咪-OPPO" + "QD0165";
            } else {
                return "QD0165";
            }
        } else if ("QD0166".equals(channle)) {
            if (id_channle == 1) {
                return "卡小咪-小米" + "QD0166";
            } else {
                return "QD0166";
            }
        } else if ("QD0167".equals(channle)) {
            if (id_channle == 1) {
                return "卡小咪-三星" + "QD0167";
            } else {
                return "QD0167";
            }
        } else {
            return "";
        }
    }

}
